﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Configuration;
using TGIApp.Model;

namespace TGIApp.Web.Controllers
{
    public class TicketTypeController : Controller
    {
        //
        // GET: /TicketType/

        public ActionResult Index()
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var ticketTypes = context.TicketTypes;
            //return View(ticketTypes);
            return View();
        }

        //
        // GET: /TicketType/Details/5

        public ActionResult Details(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var ticketType = context.TicketTypes.Where(tt => tt.Id == id).FirstOrDefault();
            //return View(ticketType);
            return View();
        }

        //
        // GET: /TicketType/Create

        public ActionResult Create()
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));

            //ViewBag.Events = context.Events.ToList();
            //ViewBag.Venues = context.Venues.ToList();
            //ViewBag.Templates = context.TicketTemplates.ToList();
            //return View();
            return View();
        }

        //
        // POST: /TicketType/Create

        [HttpPost]
        public ActionResult Create(TicketType ticketType)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));

            //context.AddToTicketTypes(ticketType);

            //EventVenue eventVenue = context.EventVenues.Where(ev => ev.Event.Id == ticketType.EventVenue.Event.Id && ev.Venue.Id == ticketType.EventVenue.Venue.Id).FirstOrDefault();
            //if (eventVenue == null)
            //{
            //    eventVenue = new EventVenue();
            //    context.AddToEventVenues(eventVenue);

            //    context.AttachTo("Events", ticketType.EventVenue.Event);
            //    context.AttachTo("Venues", ticketType.EventVenue.Venue);

            //    context.SetLink(eventVenue, "Event", ticketType.EventVenue.Event);
            //    context.SetLink(eventVenue, "Venue", ticketType.EventVenue.Venue);


            //}
            //context.SetLink(ticketType, "EventVenue", eventVenue);

            //context.AttachTo("TicketTemplates", ticketType.TicketTemplate);
            //context.SetLink(ticketType, "TicketTemplate", ticketType.TicketTemplate);


            //context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);

            //return RedirectToAction("Index");
            return View();
        }

        //
        // GET: /TicketType/Edit/5

        public ActionResult Edit(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var ticketType = context.TicketTypes.Expand("TicketTemplate,EventVenue/Event,EventVenue/Venue").Where(t => t.Id == id).FirstOrDefault();
            //ViewBag.Events = context.Events.ToList();
            //ViewBag.Venues = context.Venues.ToList();
            //ViewBag.Templates = context.TicketTemplates.ToList();
            //return View(ticketType);
            return View();
        }

        //
        // POST: /TicketType/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, TicketType ticketType)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));

            //context.AttachTo("TicketTypes", ticketType);

            //context.AttachTo("TicketTemplates", ticketType.TicketTemplate);
            //context.SetLink(ticketType, "TicketTemplate", ticketType.TicketTemplate);

            //EventVenue eventVenue = context.EventVenues.Where(ev => ev.Event.Id == ticketType.EventVenue.Event.Id && ev.Venue.Id == ticketType.EventVenue.Venue.Id).FirstOrDefault();
           
            //if (eventVenue != null)
            //{
            //    context.AttachTo("EventVenues", ticketType.EventVenue);
            //    context.AttachTo("Events", ticketType.EventVenue.Event);
            //    context.AttachTo("Venues", ticketType.EventVenue.Venue);
            //    context.SetLink(ticketType, "EventVenue", ticketType.EventVenue);
            //    context.SetLink(ticketType.EventVenue, "Event", ticketType.EventVenue.Event);
            //    context.SetLink(ticketType.EventVenue, "Venue", ticketType.EventVenue.Venue);
            //}
            //else
            //{
            //    eventVenue = new EventVenue();
            //    context.AddToEventVenues(eventVenue);
            //    context.AttachTo("Events", ticketType.EventVenue.Event);
            //    context.AttachTo("Venues", ticketType.EventVenue.Venue);
            //    context.SetLink(ticketType, "EventVenue", eventVenue);
            //    context.SetLink(eventVenue, "Event", ticketType.EventVenue.Event);
            //    context.SetLink(eventVenue, "Venue", ticketType.EventVenue.Venue);
            //}
           
            //context.UpdateObject(ticketType);
            //context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);

            //return RedirectToAction("Index");
            return View();
        }

        //
        // GET: /TicketType/Delete/5

        public ActionResult Delete(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var ticketType = context.TicketTypes.Where(t => t.Id == id).FirstOrDefault();
            //return View(ticketType);
            return View();
        }

        //
        // POST: /TicketType/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, TicketType ticketType)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //context.AttachTo("TicketTypes", ticketType);
            //context.DeleteObject(ticketType);
            //context.SaveChanges();
            //return RedirectToAction("Index");
            return View();
        }
    }
}
